<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Kunfeng\Dify\Client\DifyClientFactory;
use Kunfeng\Dify\Enum\ResponseMode;
use Kunfeng\Dify\Model\Chat\ChatMessage;
use Kunfeng\Dify\Model\Completion\CompletionRequest;
use Kunfeng\Dify\Callback\ChatStreamCallback;
use Kunfeng\Dify\Callback\CompletionStreamCallback;

// 配置
$baseUrl = 'https://api.dify.ai/v1';
$apiKey = 'your-api-key-here';

echo "=== Dify PHP 客户端基础使用示例 ===\n\n";

// 1. 创建客户端
echo "1. 创建客户端...\n";
$client = DifyClientFactory::createClient($baseUrl, $apiKey);
$chatClient = DifyClientFactory::createChatClient($baseUrl, $apiKey);
$completionClient = DifyClientFactory::createCompletionClient($baseUrl, $apiKey);

echo "✓ 客户端创建成功\n\n";

// 2. 对话型应用示例
echo "2. 对话型应用示例...\n";

// 阻塞模式对话
$message = ChatMessage::create('你好，请介绍一下自己', 'user-123')
    ->withResponseMode(ResponseMode::BLOCKING);

try {
    $response = $chatClient->sendChatMessage($message);
    echo "✓ 对话响应: " . $response->getAnswer() . "\n";
    echo "✓ 消息ID: " . $response->getMessageId() . "\n";
    echo "✓ 会话ID: " . $response->getConversationId() . "\n\n";
} catch (Exception $e) {
    echo "✗ 对话失败: " . $e->getMessage() . "\n\n";
}

// 流式模式对话
echo "3. 流式对话示例...\n";
$streamMessage = ChatMessage::create('请给我讲一个简短的故事', 'user-123')
    ->withResponseMode(ResponseMode::STREAMING);

$streamCallback = new ChatStreamCallback([
    'onMessage' => function($event) {
        echo "收到消息片段: " . $event->getAnswer();
    },
    'onMessageEnd' => function($event) {
        echo "\n✓ 消息结束，消息ID: " . $event->getMessageId() . "\n";
    },
    'onError' => function($event) {
        echo "\n✗ 错误: " . $event->getMessage() . "\n";
    }
]);

try {
    $chatClient->sendChatMessageStream($streamMessage, $streamCallback);
    echo "✓ 流式对话完成\n\n";
} catch (Exception $e) {
    echo "✗ 流式对话失败: " . $e->getMessage() . "\n\n";
}

// 4. 文本生成应用示例
echo "4. 文本生成应用示例...\n";

$completionRequest = CompletionRequest::create(
    ['content' => '茄子'],
    'user-123'
)->withResponseMode(ResponseMode::BLOCKING);

try {
    $completionResponse = $completionClient->sendCompletionMessage($completionRequest);
    echo "✓ 生成文本: " . $completionResponse->getAnswer() . "\n";
    echo "✓ 消息ID: " . $completionResponse->getMessageId() . "\n\n";
} catch (Exception $e) {
    echo "✗ 文本生成失败: " . $e->getMessage() . "\n\n";
}

// 5. 流式文本生成示例
echo "5. 流式文本生成示例...\n";

$streamCompletionRequest = CompletionRequest::create(
    ['content' => '请写一首关于春天的诗'],
    'user-123'
)->withResponseMode(ResponseMode::STREAMING);

$completionCallback = new CompletionStreamCallback([
    'onMessage' => function($event) {
        echo $event->getAnswer();
    },
    'onMessageEnd' => function($event) {
        echo "\n✓ 文本生成完成，消息ID: " . $event->getMessageId() . "\n";
    },
    'onError' => function($event) {
        echo "\n✗ 错误: " . $event->getMessage() . "\n";
    }
]);

try {
    $completionClient->sendCompletionMessageStream($streamCompletionRequest, $completionCallback);
    echo "✓ 流式文本生成完成\n\n";
} catch (Exception $e) {
    echo "✗ 流式文本生成失败: " . $e->getMessage() . "\n\n";
}

// 6. 获取应用信息
echo "6. 获取应用信息...\n";

try {
    $appInfo = $chatClient->getAppInfo();
    echo "✓ 应用名称: " . $appInfo->getName() . "\n";
    echo "✓ 应用描述: " . ($appInfo->getDescription() ?? '无') . "\n\n";
} catch (Exception $e) {
    echo "✗ 获取应用信息失败: " . $e->getMessage() . "\n\n";
}

// 7. 关闭客户端
echo "7. 关闭客户端...\n";
$client->close();
$chatClient->close();
$completionClient->close();
echo "✓ 客户端已关闭\n\n";

echo "=== 示例完成 ===\n";
